package org.gjy.m8.stream;

import java.util.Collections;
import java.util.EnumSet;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collector;

/**
 * @author 宫静雨
 * @version 1.0
 * @since 2023-08-04 16:51:17
 */
public class LinkedHashSetCollector<T> implements Collector<T, Set<T>, Set<T>> {
    private static final Set<Collector.Characteristics> CHARACTERISTICS;

    public Supplier<Set<T>> supplier() {
        return LinkedHashSet::new;
    }

    public BiConsumer<Set<T>, T> accumulator() {
        return Set::add;
    }

    public BinaryOperator<Set<T>> combiner() {
        return (left, right) -> {
            left.addAll(right);
            return left;
        };
    }

    public Function<Set<T>, Set<T>> finisher() {
        return Function.identity();
    }

    public Set<Collector.Characteristics> characteristics() {
        return CHARACTERISTICS;
    }

    static {
        CHARACTERISTICS = Collections.unmodifiableSet(EnumSet.of(Characteristics.IDENTITY_FINISH));
    }
}
